package zhr.os;

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int numOfResources = 3; // 假设有3种资源
        // 10 5 7
        int[] available = {10, 5, 7}; // 可用资源
        /*// 每个进程的最大需求
        int[][] max = {
                {7, 5, 3},
                {3, 2, 2},
                {9, 0, 2},
                {2, 2, 2},
                {4, 3, 3}
        };
        // 每个进程的已分配资源
        int[][] allocation = {
                {0, 1, 0},
                {2, 0, 0},
                {3, 0, 2},
                {2, 1, 1},
                {0, 0, 2}
        };*/
// 每个进程的最大需求
        int[][] max = {
                {10, 4, 7},
                {3, 3, 2},
                {7, 4, 5},
                {5, 3, 2},
                {7, 4, 3}
        };
        // 每个进程的已分配资源
        int[][] allocation = {
                {0, 1, 0},
                {2, 0, 0},
                {3, 0, 2},
                {2, 1, 1},
                {0, 0, 2}
        };
        //可利用的资源
        for (int i = 0; i < numOfResources; i++) {
            for (int j = 0; j < max.length; j++) {
                available[i] = available[i] - allocation[j][i];
            }
        }
        BankerAlgorithm bankerAlgorithm = new BankerAlgorithm(numOfResources, available, max, allocation);

        /*// 打印初始状态
        bankerAlgorithm.printState();

        // 进程P1请求资源
        int processID = 1; // 进程ID
        int[] request = {1, 2, 2}; // 请求的资源数量
        bankerAlgorithm.requestResources(processID, request);

        // 再次打印状态
        bankerAlgorithm.printState();*/
        // 获取并打印安全序列
        int[] safeSequence = bankerAlgorithm.findSafeSequence();
        System.out.println("安全序列是：");
        for (int i : safeSequence) {
            System.out.print("P" + i + " ");
        }

    }
}